package cn.wyu.blogrest.controller;

import cn.wyu.blogrest.entity.User;
import cn.wyu.blogrest.repository.AuthRepository;
import cn.wyu.blogrest.repository.UserRepository;
import cn.wyu.blogrest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.Set;

/**
 * Created by ajinlong on 2021/10/26.
 */

@RestController
@RequestMapping(value = "/friendship")
@Transactional
public class FriendshipController {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private AuthRepository authRepository;

    /**
     * 关注好友
     */
    @RequestMapping(value = "/create/{friendId}")
    public Set<User> create(@RequestHeader("accessToken") String accessToken, @PathVariable int friendId){
        User user = this.authRepository.findByAccessToken(accessToken).getUser();

        return  this.userService.createFriendship(user,friendId).getFriends();
    }


    /**
     * 取消关注
     */

    @RequestMapping(value = "/destory/{friendId}")
    public Set<User> destory(@RequestHeader("accessToken") String accessToken,@PathVariable int friendId){
        User user = this.authRepository.findFirstByAccessToken(accessToken).getUser();
        return this.userService.destoryFriendship(user,friendId).getFriends();
    }

    /**
     * 获取用户粉丝数
     */

    @RequestMapping(value = "/follower/{userId}",method = RequestMethod.GET)
    public Set<User> followerGet(@PathVariable int userId){
        return  this.userRepository.findById(userId).getFollowers();
    }
}
